package cn.iocoder.yudao.module.wms.controller.admin.inventoryrecord;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.inventoryrecord.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.inventoryrecord.InventoryRecordDO;
import cn.iocoder.yudao.module.wms.service.inventoryrecord.InventoryRecordService;

@Tag(name = "管理后台 - 库存记录")
@RestController
@RequestMapping("/wms/inventory-record")
@Validated
public class InventoryRecordController {

    @Resource
    private InventoryRecordService inventoryRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建库存记录")
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:create')")
    public CommonResult<String> createInventoryRecord(@Valid @RequestBody InventoryRecordSaveReqVO createReqVO) {
        return success(inventoryRecordService.createInventoryRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存记录")
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:update')")
    public CommonResult<Boolean> updateInventoryRecord(@Valid @RequestBody InventoryRecordSaveReqVO updateReqVO) {
        inventoryRecordService.updateInventoryRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:delete')")
    public CommonResult<Boolean> deleteInventoryRecord(@RequestParam("id") String id) {
        inventoryRecordService.deleteInventoryRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:query')")
    public CommonResult<InventoryRecordRespVO> getInventoryRecord(@RequestParam("id") String id) {
        InventoryRecordDO inventoryRecord = inventoryRecordService.getInventoryRecord(id);
        return success(BeanUtils.toBean(inventoryRecord, InventoryRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存记录分页")
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:query')")
    public CommonResult<PageResult<InventoryRecordRespVO>> getInventoryRecordPage(@Valid InventoryRecordPageReqVO pageReqVO) {
        PageResult<InventoryRecordDO> pageResult = inventoryRecordService.getInventoryRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InventoryRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "出入库记录 Excel")
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInventoryRecordExcel(@Valid InventoryRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InventoryRecordDO> list = inventoryRecordService.getInventoryRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(
                response,
                "出入库记录.xls",
                "数据",
                InventoryRecordExcelVO.class,
                BeanUtils.toBean(list, InventoryRecordExcelVO.class));
    }

    @GetMapping("/analyze/{type}")
    @Operation(summary = "出入库分析")
    @PreAuthorize("@ss.hasPermission('wms:inventory-record:analyze')")
    public CommonResult<List<InventoryRecordAnalyzeVO>> analyze(@PathVariable Integer type) {
        List<InventoryRecordAnalyzeVO> list = inventoryRecordService.analyze(type);
        return success(list);
    }
}